home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue54 / Persist / DBNextOID.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-01-21  |  3.2 KB  |  141 lines

  1. { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.   Purpose: Create a new Object ID (OID) which will be unique within
  3.            the database.
  4.  
  5.   Revision History:
  6.   Sept 1999, PWH, Created
  7.  
  8. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * }
  9.  
  10.   { ToDo 1 -cBOM: Break NextOID into two (or three?) units }
  11.   
  12. unit DBNextOID;
  13.  
  14. interface
  15. uses
  16.   classes
  17.   ,tiPtnVisitor
  18.   ,tiPtnVisitorDB
  19.   ,tiPtnVisitorMgr
  20.   ;
  21.  
  22. type
  23.  
  24.   TNextOID = class( TVisitedAbs )
  25.   private
  26.     FIntHigh : integer ;
  27.     FIntLow : integer ;
  28.     function GetNextOID : integer ;
  29.   public
  30.     constructor create ; override ;
  31.     property    NextOID : integer read GetNextOID ;
  32.     property    NextOIDHigh : integer read FIntHigh write FIntHigh ;
  33.   end ;
  34.  
  35.   TVisGetNextOID = class( TVisDBSelect )
  36.   protected
  37.     function    AcceptVisitor : boolean ; override ;
  38.     procedure   Init            ; override ;
  39.     procedure   MapRowsToObject ; override ;
  40.   public
  41.   end ;
  42.  
  43.   TVisIncNextOID = class( TVisDBUpdate )
  44.   protected
  45.     function    AcceptVisitor : boolean ; override ;
  46.     procedure   Init            ; override ;
  47.   public
  48.   end ;
  49.  
  50. function gNextOID : TNextOID ;
  51.  
  52. implementation
  53. uses
  54.   cAdrs
  55.   ,SysUtils
  56.   ;
  57.  
  58. const
  59.   cuiLow = 10 ;
  60.  
  61. var
  62.   uNextOID : TNextOID ;
  63.  
  64. //------------------------------------------------------------------------------
  65. function gNextOID : TNextOID ;
  66. begin
  67.   if uNextOID = nil then
  68.     uNextOID := TNextOID.Create ;
  69.   result := uNextOID ;
  70. end ;
  71.  
  72. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  73. // *
  74. // * TNextOID
  75. // *
  76. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  77. constructor TNextOID.create;
  78. begin
  79.   inherited create ;
  80.   FIntHigh := 0 ;
  81.   FIntLow  := 0 ;
  82. end;
  83.  
  84. //------------------------------------------------------------------------------
  85. function TNextOID.GetNextOID: integer;
  86. begin
  87.   if FIntHigh = 0 then
  88.     gVisitorCache.Execute( cgsGetNextOIDHigh, self )  ;
  89.  
  90.   result := FIntHigh * cuiLow + FIntLow ;
  91.  
  92.   inc( FIntLow ) ;
  93.   if FIntLow >= cuiLow-1 then begin
  94.     FIntHigh := 0 ;
  95.     FIntLow := 0 ;
  96.   end ;
  97.  
  98. end;
  99.  
  100.  
  101. //------------------------------------------------------------------------------
  102. function TVisGetNextOID.AcceptVisitor: boolean;
  103. begin
  104.   result := ( Visited is TNextOID ) ;
  105. end;
  106.  
  107. procedure TVisGetNextOID.Init;
  108. begin
  109.   Query.SQL.Text := 'select Next_OID from Next_OID' ;
  110. end;
  111.  
  112. procedure TVisGetNextOID.MapRowsToObject;
  113. begin
  114.   TNextOID( Visited ).NextOIDHigh := Query.FieldByName( 'Next_OID' ).AsInteger ;
  115. end;
  116.  
  117. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  118. // *
  119. // * TVisIncNextOID
  120. // *
  121. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  122. function TVisIncNextOID.AcceptVisitor: boolean;
  123. begin
  124.   result := ( Visited is TNextOID ) ;
  125. end;
  126.  
  127. procedure TVisIncNextOID.Init;
  128. begin
  129.   Query.SQL.Text := 'update Next_OID set Next_OID = Next_OID + 1' ;
  130. end;
  131.  
  132. initialization
  133.   gVisitorCache.RegisterVisitor( cgsGetNextOIDHigh, TVisGetNextOID ) ;
  134.   gVisitorCache.RegisterVisitor( cgsGetNextOIDHigh, TVisIncNextOID ) ;
  135.  
  136. finalization
  137.   uNextOID.Free ;
  138.  
  139. end.
  140.  
  141.